package com.spring.learn.controller;

import com.other.TwoCodeUtils;
import com.spring.learn.ValidVO;
import com.spring.learn.model.SystemEntity;
import com.spring.learn.service.NoAnnoService;
import com.spring.learn.service.UserService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by baiguantao on 2017/4/24.
 */
/*@PropertySource("classpath:/jdbc2.properties")*/
@Controller
@RequestMapping(value = {"/index"})
public class FirstController implements InitializingBean{
    @RequestMapping("/hello")
    public @ResponseBody String hello() throws Exception {
        System.out.println("hello .....");
        String userId= UUID.randomUUID().toString();
        //生成名医二维码
        String twocode="";
        String content="http://www.baidu.com";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("/yyyy/MM/dd/");
        String tempath="twocode"+simpleDateFormat.format(new Date());
        String destpat="d:/upload/"+tempath;
        twocode="image/"+tempath+userId+".jpg";
        TwoCodeUtils.ysencode(content,null,destpat,userId);
        return "hello";
    }
    // /42;q=11/pets/21;q=22

    /**
     * <mvc:annotation-driven enable-matrix-variables="true"/>
     * @param ownerId
     * @param petId
     * @param q1
     * @param q2
     *
     * result:
    ownerId:=42
    q1:=11
    petId:=21
    q2:=22
     */
    @RequestMapping("/{ownerId}/pets/{petId}")
    public void findPet(
            @PathVariable String ownerId,
            @PathVariable String petId,
            @MatrixVariable(name="q", pathVar="ownerId") int q1,
            @MatrixVariable(name="q", pathVar="petId") int q2) {

        System.out.println("ownerId:="+ownerId);
        System.out.println("q1:="+q1);
        System.out.println("petId:="+petId);
        System.out.println("q2:="+q2);

    }

    @Autowired
    private UserService userService;
    /**
     * http://localhost:8086/index/valid/
     * 利用hibernate进行格式化验证
     * @param system
     */
    @RequestMapping(value = "/valid",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getUser(@Valid SystemEntity system, BindingResult bindingResult){
        System.out.println(system.toString());
        userService.testAspectJ();
        if(bindingResult.hasErrors()){
            StringBuffer sb = new StringBuffer();
            sb.append("错误信息：");
            for(ObjectError objectError : bindingResult.getAllErrors()){
                sb.append(((FieldError)objectError).getField() +" : ").append(objectError.getDefaultMessage().toString());
            }
            return sb.toString();
        }else {
            return system.toString();
        }
    }
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder){
        webDataBinder.addValidators(new ValidVO());
    }
    @Autowired
    private NoAnnoService ricky;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("我执行了吗？bean的范围");
        System.out.println(userService.toString());
        System.out.println(ricky.toString());
    }
}
